<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="演员姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入演员姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="queryParams.gender" placeholder="请选择性别" clearable>
          <el-option label="男" value="0" />
          <el-option label="女" value="1" />
          <el-option label="未知" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['movie:actor:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['movie:actor:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['movie:actor:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['movie:actor:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="actorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="演员ID" align="center" prop="actorId" width="80" />
      <el-table-column label="头像" align="center" prop="avatar" width="100">
        <template #default="scope">
          <el-image
            style="width: 60px; height: 60px; border-radius: 50%;"
            :src="scope.row.avatar"
            :preview-src-list="[scope.row.avatar]"
            fit="cover"
            v-if="scope.row.avatar"
          >
            <template #error>
              <div class="image-slot">
                <el-icon><UserFilled /></el-icon>
              </div>
            </template>
          </el-image>
          <div v-else class="image-slot" style="width: 60px; height: 60px; border-radius: 50%;">
            <el-icon><UserFilled /></el-icon>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="英文名" align="center" prop="nameEn" :show-overflow-tooltip="true" />
      <el-table-column label="中文别名" align="center" prop="nameMore" :show-overflow-tooltip="true" />
      <el-table-column label="英文别名" align="center" prop="nameEnMore" :show-overflow-tooltip="true" />
      <el-table-column label="性别" align="center" prop="gender" width="80">
        <template #default="scope">
          <el-tag v-if="scope.row.gender === '0'" type="primary">男</el-tag>
          <el-tag v-else-if="scope.row.gender === '1'" type="danger">女</el-tag>
          <el-tag v-else type="info">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="出生日期" align="center" prop="birthday" width="120">
        <template #default="scope">
          <span>{{ parseDate(scope.row.birthday) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="逝世日期" align="center" prop="deathDay" width="120">
        <template #default="scope">
          <span>{{ parseDate(scope.row.deathDay) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="国家/地区" align="center" prop="country.name" width="120" />
      <el-table-column label="出生地" align="center" prop="bornPlace" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['movie:actor:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['movie:actor:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改演员对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="actorRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="演员姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入演员姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文名" prop="nameEn">
              <el-input v-model="form.nameEn" placeholder="请输入英文名" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="中文别名" prop="nameMore">
              <el-input v-model="form.nameMore" placeholder="多个别名用逗号分隔" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文别名" prop="nameEnMore">
              <el-input v-model="form.nameEnMore" placeholder="多个别名用逗号分隔" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-radio-group v-model="form.gender">
                <el-radio label="0">男</el-radio>
                <el-radio label="1">女</el-radio>
                <el-radio label="2">未知</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="birthday">
              <el-date-picker
                v-model="form.birthday"
                type="date"
                placeholder="选择出生日期"
                value-format="YYYY-MM-DD"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="逝世日期" prop="deathDay">
              <el-date-picker
                v-model="form.deathDay"
                type="date"
                placeholder="选择逝世日期"
                value-format="YYYY-MM-DD"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="国家/地区" prop="countryId">
              <el-select v-model="form.countryId" placeholder="请选择国家/地区" clearable style="width: 100%">
                <el-option
                  v-for="item in countryOptions"
                  :key="item.countryId"
                  :label="item.name"
                  :value="item.countryId"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="出生地" prop="bornPlace">
              <el-input v-model="form.bornPlace" placeholder="请输入出生地" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="头像URL" prop="avatar">
              <el-input v-model="form.avatar" placeholder="请输入头像URL">
                <template #append>
                  <el-button icon="Upload">上传</el-button>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="个人简介" prop="brief">
              <el-input
                type="textarea"
                v-model="form.brief"
                placeholder="请输入个人简介"
                :rows="4"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MovieActor">
import { listActor, getActor, delActor, addActor, updateActor } from "@/api/movie/actor";
import { listCountry } from "@/api/basic/country";
import { getCurrentInstance, ref, reactive, toRefs } from 'vue';
import { UserFilled } from '@element-plus/icons-vue';

const { proxy } = getCurrentInstance();

const actorList = ref([]);
const countryOptions = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    gender: null
  },
  rules: {
    name: [
      { required: true, message: "演员姓名不能为空", trigger: "blur" }
    ],
    gender: [
      { required: true, message: "性别不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 格式化日期 */
function parseDate(date) {
  if (!date) return '-';
  return date.split(' ')[0];
}

/** 查询演员列表 */
function getList() {
  loading.value = true;
  listActor(queryParams.value).then(response => {
    actorList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询国家列表 */
function getCountryList() {
  listCountry().then(response => {
    countryOptions.value = response.rows || [];
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    actorId: null,
    name: null,
    nameEn: null,
    nameMore: null,
    nameEnMore: null,
    gender: "2",
    birthday: null,
    deathDay: null,
    countryId: null,
    bornPlace: null,
    avatar: null,
    brief: null
  };
  proxy.resetForm("actorRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.actorId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  getCountryList();
  open.value = true;
  title.value = "添加演员";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  getCountryList();
  const _actorId = row.actorId || ids.value
  getActor(_actorId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改演员";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["actorRef"].validate(valid => {
    if (valid) {
      if (form.value.actorId != null) {
        updateActor(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addActor(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _actorIds = row.actorId || ids.value;
  proxy.$modal.confirm('是否确认删除演员编号为"' + _actorIds + '"的数据项？').then(function() {
    return delActor(_actorIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('movie/actor/export', {
    ...queryParams.value
  }, `movie_actor_${new Date().getTime()}.xlsx`)
}

getList();
</script>

<style scoped>
.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
  font-size: 24px;
}
</style>